<?php

namespace app\api\controller;
use think\admin\Controller;
use app\api\model\DataAddresser;
use app\api\model\DataCity;
use app\api\model\ShopOrder;
use app\data\model\ShopOrderSend;
use expressage\Kd100;
use think\Request;
class Express extends Controller
{
    protected $default_type = 'json';
    public function __construct(Request $request){
        $this->request = $request;
    }
    /**
     * 订单跟踪
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderTrace(){
        $order_no = input('order_no','');
        $tokeninfo = $this->request->tokeninfo;
        if(empty($tokeninfo)){
            return \Tools::returnMsg(false,array(
                'code'  => '100001',
                'message'=>'未登录  请先登录'
            ),$this->default_type);
        }
        if(empty($order_no)) {
            return \Tools::returnMsg(false,array(
                'code'  => '10004',
                'message'=>'参数有误'
            ),$this->default_type);
        }
        $order = ShopOrder::mk()->where([
            'uuid'  => $tokeninfo['id'],
            'order_no'=>$order_no
        ])->find();
        if (empty($order)){
            return \Tools::returnMsg(false,array(
                'code'  => '10004',
                'message'=>'订单异常'
            ),$this->default_type);
        }
        $order = $order->toArray();
        $data = [];
        //初始化数据
        $order_tips = '已下单';
        $order_traces = [];
        $traces = [];//物流轨迹
        $shipment = [//发货
            'title' => '已发货',
            'tips' => '',
            'time' => '',
        ];
        $finish = [//交易完成
            'title' => '',
            'tips' => '',
            'time' => '',
        ];
        $data['trade_no'] = $order['trade_no'];
        $orderSend = ShopOrderSend::getDetail('address_code,address_name,address_phone,address_content,
            company_code,send_number,send_id',
            array(
                'order_no'  => $order['order_no']
            ));
        $addresser = DataAddresser::getDetail('*',['id'=>$orderSend['send_id']]);
        $send_address = '';
        if(empty($addresser) === FALSE){
            $province = DataCity::getProvince($addresser['province_id']);
            $city = DataCity::getCity($addresser['city_id']);
            $send_address .= $province;
            $send_address .= $city;
        }
        $data['mobile'] = $orderSend['address_phone'];
        $address = explode('市',$orderSend['address_content']);
        $data['to_address'] = $address[0].'市';
        $data['from_address'] = $send_address;
        $data['express_no'] = $orderSend['company_code'];
        $key = sysconf('kd100_appkey');// 客户授权key
        $kd100_customer = sysconf('kd100_customer');// 查询公司编号
        $expressage = (new Kd100($kd100_customer, $key));
        $expressage->logistics($data);
//        $res_data = PrintOrder::synQuery($data);
        $order_delivery = ShopOrderSend::getDetail('company_name',['order_no'=>$order['order_no']]);
        $traces = $expressage->logisticsFormat();
        //获取不到物流轨迹时
        if ($traces == false) {
            $error = $expressage->getError();
            $error = json_decode($error,true);
            if($error['result'] == false){
                $traces[] = [$error['message']];
            }
        } else {
            foreach ($traces as &$item) {
                $item = array_values(array_unique($item));
            }
        }
        //待收货
            if ($order['status'] == 5) {
                $shipment['tips'] = '商品已出库';
                $shipment['time'] = $order['trade_send_time'];
            }
            //确认收货
            if ($order['status'] == 6) {
                $order_tips = '交易完成';
                $finish['title'] = '交易完成';
                $finish['tips'] = '订单交易完成';
                $finish['time'] = $order['update_at'];
            }
            //数据合并
            $order_traces = [
                'take' => [
                    'contacts' => $order['username'],
                    'mobile' => $order['phone'],
                    'address' => $order['address'],
                ],
                'finish' => $finish,
                'delivery' => [
                    'title' => '运输中',
                    'traces' => $traces
                ],
                'shipment' => $shipment,
                'buy' => [
                    'title' => '已下单',
                    'tips' => '订单提交成功',
                    'time' => $order['payment_datetime']
                ],
            ];
            return \Tools::returnMsg(true,$order_traces,$this->default_type);
    }
}
